package com.ruoyi.utils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 通用分页逻辑处理类
 *
 * @author 周志雄
 * @date 2024-03-09
 */
public class PaginationHelper {

    public static <T> PageResult<T> paginateList(List<T> allItems, PageRequest pageRequest) {
        int totalSize = allItems.size();
        int totalPages = (int) Math.ceil((double) totalSize / (double) pageRequest.getPageSize());

        int pageNum = pageRequest.getPageNum();
        int pageSize = pageRequest.getPageSize();

        int fromIndex = (pageNum - 1) * pageSize;
        int toIndex = Math.min(fromIndex + pageSize, totalSize);

        List<T> pageData = allItems.subList(fromIndex, toIndex);

        return new PageResult<>(totalSize, totalPages, pageData, pageNum, pageSize);
    }
}
